/*
 * PayResultFragment.java
 * business
 *
 * Created by ChenTao on 2017/2/28.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.orderresult;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.ui.adapter.PayResultAdapter;
import com.yidiandao.business.ui.widget.dialog.ShareDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 支付结果页面View
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/21 16:07
 */
public class OrderResultFragment extends SuperBaseFragment implements OrderResultContract.View {

    private OrderResultContract.Presenter mPresenter;

    private PayResultAdapter mAdapter;

    private Toolbar mToolbar;

    private ImageButton mIbtnShare;

    private ListView mListView;

    private TextView mTvTitle;

    public static OrderResultFragment getInstance(Bundle bundle) {
        OrderResultFragment fragment = new OrderResultFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setPresenter(OrderResultContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return OrderResultFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mIbtnShare = (ImageButton) mRootView.findViewById(R.id.ibtn_share);
        mListView = (ListView) mRootView.findViewById(R.id.list_view);
        mTvTitle = (TextView) mRootView.findViewById(R.id.tv_title);

        initListView();
        initListeners();

        mPresenter.extraBundle(getArguments());
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });

        mIbtnShare.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        doShare();
                    }
                });
    }

    private void doShare() {
        if (mPresenter.getShareEntity() == null) {
            return;
        }
        ShareDialog shareDialog = new ShareDialog(mActivity);
        shareDialog.show();
        shareDialog.setShareLinkData(mPresenter.getShareEntity());
    }

    private void initListView() {
        mAdapter = new PayResultAdapter(mActivity);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void setDisplayListItems(List<DisplayListItem> displayListItems) {
        mAdapter.setData(displayListItems);
    }

    @Override
    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    @Override
    public void setShareIconVisibility(boolean success) {
        mIbtnShare.setVisibility(success ? View.VISIBLE : View.GONE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_result;
    }

    @Override
    protected void startLoadData() {
        mPresenter.loadPayResult();
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }
}
